package com.a3dweijing.wetouch2.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.LoginMsg;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.a3dweijing.wetouch2.tools.SharedPreUtil;
import com.google.gson.Gson;

/**
 * Created by yxd on 2017/10/13.
 */

public class PasswordLoginActivity extends BaseActivity {
    private TextView phoneNumTxv;
    private TextView forgetPwdtxt;
    private EditText passwordEdt;
    private TextView nextBtn;
    private Handler handler;
    private String phoneNumStr;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_login);
        phoneNumStr=getIntent().getStringExtra("phone_number");
        phoneNumTxv= (TextView) findViewById(R.id.phone_number_txv);
        forgetPwdtxt= (TextView) findViewById(R.id.login_forget_password_txv);
        passwordEdt= (EditText) findViewById(R.id.login_password_edt);
        nextBtn= (TextView) findViewById(R.id.login_next_btn);
        phoneNumTxv.setText(phoneNumStr);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                endPopupwindow();
                switch (msg.what){
                    case 100:
                        LoginMsg loginMsg=new Gson().fromJson(msg.obj.toString(),LoginMsg.class);
                        if (loginMsg.isFlag())
                        {
                            Singleton.getInstance().setUser(loginMsg.getUser());
                            SharedPreUtil.put(getBaseContext(),"username",phoneNumStr);
                            SharedPreUtil.put(getBaseContext(),"userpwd",passwordEdt.getText().toString().trim());
                            Intent intent=new Intent(PasswordLoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(getBaseContext(), loginMsg.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 404:
                        Toast.makeText(getBaseContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                }

            }
        };
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String pwd=passwordEdt.getText().toString().trim();
                if (pwd!=null && pwd.length()>0)
                {
                    new LoginThread(phoneNumStr,pwd).start();
                    startPopupwindow(getBaseContext(),"登录中，请稍后...");
                }else {
                    Toast.makeText(getBaseContext(),"请输入密码", Toast.LENGTH_SHORT).show();
                }
            }
        });
        forgetPwdtxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(PasswordLoginActivity.this,ForgetPasswordActivity.class);
                intent.putExtra("phone_number",phoneNumStr);
                startActivity(intent);

            }
        });

    }

    private class LoginThread extends Thread
    {
        String phone_number;
        String password;

        public LoginThread(String phone_number, String password) {
            this.phone_number = phone_number;
            this.password = password;
        }

        @Override
        public void run() {
            super.run();
            String resulet=new Getdataimpl().remotUserLogin(phone_number,password);
            Log.e("--",resulet);
            if (resulet!=null && resulet.length()>7)
            {
                handler.sendMessage(handler.obtainMessage(100,resulet));
            }else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode== KeyEvent.KEYCODE_BACK)
        {
            Intent backOne=new Intent(PasswordLoginActivity.this,LoginActivity.class);
            startActivity(backOne);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
